package com.tom.demo.web.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.tom.demo.common.response.Result;
import com.tom.demo.dto.LoginDto;
import com.tom.demo.service.system.SysUserService;
import com.tom.demo.vo.RouterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = {"用户管理"})
@RestController
@RequestMapping("/user")
public class SysUserController {


    @Resource
    private SysUserService sysUserService;

    @PostMapping(path = {"/login"})
    @ApiOperation(value = "用户登录")
    public Result login(@RequestBody LoginDto loginDto){
        return sysUserService.login(loginDto);
    }


    @GetMapping(path = {"/getTokenInfo"})
    @ApiOperation(value = "获取sa-token中信息")
    public Result tokenInfo(){
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        return Result.success(tokenInfo);
    }

    @GetMapping(path = {"/getUserInfo"})
    @ApiOperation(value = "获取用户信息")
    public Result getUserInfo(){
        return sysUserService.getUserInfo();
    }

    @GetMapping(path = {"/getPermission"})
    @ApiOperation(value = "获取用户权限信息")
    public Result getPermission(){
        return sysUserService.getPermission();
    }

    @GetMapping(path = {"/getRouters"})
    @ApiOperation(value = "获取用户路由信息")
    public Result getRouters(){
        List<RouterVo> routerVos = sysUserService.getRouters();
        return Result.success(routerVos);
    }

    @GetMapping(path = {"/logout"})
    @ApiOperation(value = "退出登录")
    public Result logout(){
        StpUtil.logout();
        return Result.success();
    }


    @GetMapping(path = "/findPermission")
    @ApiOperation(value = "测试必须有某一个权限才可以访问")
    @SaCheckPermission(value = {"system:user:list","system:test:permission"},mode = SaMode.OR)
    public Result findPermission(){
        return Result.success();
    }

    @GetMapping(path = "/findRole")
    @ApiOperation(value = "测试必须有某一个角色才可以访问")
    @SaCheckRole(value = {"common","superAdmin"},mode = SaMode.OR)
    public Result findRole(){
        return Result.success();
    }

}
